﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL.DHelpers;
using System.Collections.Generic;
using System.Data.SqlTypes;

namespace DAL.DHelpers
{
    public class MappingTable
    {
        DatabaseManager db = new DatabaseManager(DatabaseProvider.SQLServer);

        #region Tên bảng
        private String tableName;
        public String TableName
        {
            set { tableName = value; }
            get { return "[" + tableName + "]"; }
        }
        #endregion

        #region Thông tin các cột trong bảng
        private Dictionary<String, Column> columns = new Dictionary<String, Column>();
        public Dictionary<String, Column> Columns
        {
            set { columns = value; }
            get { return columns; }
        }
        #endregion
     
        #region Thông tin các cột khóa chính
        private Dictionary<String, Column> primaryKeys = new Dictionary<String, Column>();
        public Dictionary<String, Column> PrimaryKeys
        {
            set { primaryKeys = value; }
            get { return primaryKeys; }
        }
        #endregion

        public Boolean HasColumnIsIdentity { get; set; } 

        public MappingTable(String tableName, bool indent)
        {
            this.tableName = tableName;
            
            this.HasColumnIsIdentity = indent;
            this.GetColumnInformation();
        }
        private void GetColumnInformation()
        {
            DataTable table = this.db.ExecuteDataTable("SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE,COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') [Identity]  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + this.tableName + "'");
            if (table.Rows.Count != 0)
            {
                this.columns = new Dictionary<String, Column>();
                Column column = null;
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    column = new Column();
                    column.ColumnName = table.Rows[i]["COLUMN_NAME"].ToString();
                    column.DataType = column.ConvertFromString(table.Rows[i]["DATA_TYPE"].ToString());
                    column.IsNullAble = table.Rows[i]["IS_NULLABLE"].ToString().Equals("NO") ? false : true;
                    column.IsIdentity = table.Rows[i]["Identity"].ToString().Equals("1") ? true : false;
                    if (column.IsIdentity)
                        this.HasColumnIsIdentity = true;
                    this.columns.Add(column.ColumnName, column);
                }
            }

            table = this.db.ExecuteDataTable("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS c INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE k ON c.CONSTRAINT_NAME = k.CONSTRAINT_NAME WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' AND c.TABLE_NAME = '" + this.tableName + "'");
            if (table.Rows.Count != 0)
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    this.columns[table.Rows[i][0].ToString()].IsPrimaryKey = true;
                    this.primaryKeys.Add(table.Rows[i][0].ToString(), this.columns[table.Rows[i][0].ToString()]);
                }
        }

    }
   
   
    public class Column
    {
        private String columnName;
        public String ColumnName
        {
            set { columnName = value; }
            get { return columnName; }
        }
        private String value;
        public String Value
        {
            set { this.value = value; }
            get { return value; }
        }
        private DataType.DataTypeSql dataType;
        public DataType.DataTypeSql DataType
        {
            set { dataType = value; }
            get { return dataType; }
        }

        private Boolean isPrimaryKey = false;
        public Boolean IsPrimaryKey
        {
            set { isPrimaryKey = value; }
            get { return isPrimaryKey; }
        }

        private Boolean isNullAble = false;
        public Boolean IsNullAble
        {
            set { isNullAble = value; }
            get { return isNullAble; }
        }

        private Boolean isIdentity = false;
        public Boolean IsIdentity
        {
            set { isIdentity = value; }
            get { return isIdentity; }
        }
        public Column ()
            {
            }
        public Column (string name , string value ,DataType.DataTypeSql type ,Boolean isPK,Boolean isNA,Boolean isID)
        {
            this.ColumnName = name;
            this.value = value;
            this.dataType = type;
            this.isIdentity = isID;
            this.isNullAble = isNA;
            this.isPrimaryKey = isPK;
        }
        public Column(string name, string value, DataType.DataTypeSql type)
        {
            this.ColumnName = name;
            this.value = value;
            this.dataType = type;
           
        }
       public object GetValues ()
        {
               return ParamsCreator.instance.GetValue(this.DataType, this.Value);
        }
        public DataType.DataTypeSql ConvertFromString(String type)
        {
                return ParamsCreator.instance.ConvertFromString(type);
        }
    }

    public class RowValue
    {

        public static Dictionary<String, object> RowValues = new Dictionary<String, object>();
        public RowValue(string[] names, object[] values)
        {
            for (int i = 0; i < names.Length; i++)
                RowValues.Add(names[i], values[i]);
          
        }
       
    }

}
